package cn.edu.thsis.servlet;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.thsis.pojo.newsinfo;
import cn.edu.thsis.pojo.thsis;
import cn.edu.thsis.service.UserService;
import cn.edu.thsis.service.impl.UserServiceImpl;

/**
 * Servlet implementation class StuSvlt
 */
@WebServlet("/StuSvlt")
public class StuSvlt extends HttpServlet {
	UserService tea = new UserServiceImpl();
	UserService stu = new UserServiceImpl();
	UserService th = new UserServiceImpl();
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;utf-8");
		String stu_id = req.getParameter("stu_id");
		String thsis_id = req.getParameter("thsis_id");
		String oper = req.getParameter("oper");
		ResultSet rs = null;
		if ("enrol".equals(oper)) {
			try {
				doEnrol(req,resp,stu_id,thsis_id);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			resp.sendRedirect("student/thsis_list_stu.jsp");
		}else if ("allthes".equals(oper)) {
			try {
				allthes(req,resp);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	//查看所有论题
	private void allthes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, SQLException {
		List<thsis> thes = th.allthes();
		req.setAttribute("thes", thes);
		req.getRequestDispatcher("student/thsis_list_stu.jsp").forward(req, resp);
		
	}

	//预约论题
	private void doEnrol(HttpServletRequest req, HttpServletResponse resp, String stu_id, String thsis_id) throws SQLException {
		int num = stu.doEnrolService(thsis_id,stu_id);
		if (num > 0) {
			HttpSession hs = req.getSession();
			hs.setAttribute("thsis_id", thsis_id);
		}
	}


	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}
